<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    .cls {
      width: 200px;
      height: 200px;
      background-color: pink;
    }
  </style>
</head>

<body>
  <!-- 
    创建属性
    let att = document.createAttribute("class");
    设置属性值
    att.value= "demoClass"
    给元素添加上属性
    element.setAttributeNode(att);
  -->
  <div></div>
</body>
<script>
  let att = document.createAttribute("class");
  att.value = "cls";
  document.querySelector("div").setAttributeNode(att);
</script>

</html>